import { useUserStoreHook } from '@/store/modules/user'
import { difference, intersection, isEmpty, isString } from 'lodash-es'

export default function useAuth() {
  const { permissions } = useUserStoreHook()

  /**
   * 是否有按钮级别的权限
   * @param value 指令值
   * @param arg 指令参数
   * @returns Boolean false未没有权限，true为有权限
   */
  function hasPermission(value: string | Array<string>, arg = 'and'): boolean {
    if (!value) return true

    if (isEmpty(permissions)) return false

    // 管理员
    if (permissions.includes('*:*:*')) return true

    if (isString(value)) return permissions.includes(value)

    if (arg == 'or') {
      return intersection(value, permissions).length > 0
    }

    return difference(value, permissions).length === 0
  }

  return { hasPermission }
}
